package com.cosc716.restaurant.domain.collections;

import java.util.ArrayList;
import java.util.Iterator;

import com.cosc716.restaurant.domain.Table;

public class Tables {

	private ArrayList<Table> tables;
	
	public Tables() {
		tables = new ArrayList<Table>();
		
		// setup initial tables
		Table table;
		
		table = new Table(0, 2);
		this.addTable(table);
		
		table = new Table(1, 2);
		this.addTable(table);
		
		table = new Table(2, 4);
		this.addTable(table);
		
		table = new Table(3, 4);
		this.addTable(table);
		
		table = new Table(4, 4);
		this.addTable(table);
		
		table = new Table(5, 8);
		this.addTable(table);
	}
	
	private void addTable(Table table) {
		tables.add(table);
	}
	
	public ArrayList<Table> getTables() {
		return tables;
	}
	
	public void clearAll() {
		for (Table t : this.tables) {
			t.clearTable();
		}
	}
	
	public static EmptyTableIterator getEmptyTableIterator(Tables tables) {
		return new EmptyTableIterator(tables);
	}
	
	public EmptyTableIterator getEmptyTableIterator() {
		return getEmptyTableIterator(this);
	}
	
	public static InUseTableIterator getInUseTableIterator(Tables tables) {
		return new InUseTableIterator(tables);
	}
	
	public InUseTableIterator getInUseTableIterator() {
		return getInUseTableIterator(this);
	}

	public static OrderedTableIterator getOrderedTableIterator(Tables tables) {
		return new OrderedTableIterator(tables);
	}
	
	public OrderedTableIterator getOrderedTableIterator() {
		return getOrderedTableIterator(this);
	}
	
	public static UnorderedTableIterator getUnorderedTableIterator(Tables tables) {
		return new UnorderedTableIterator(tables);
	}
	
	public UnorderedTableIterator getUnorderedTableIterator() {
		return getUnorderedTableIterator(this);
	}
}

class EmptyTableIterator implements Iterator<Table> {
	
	private ArrayList<Table> tables;
	private int position = 0;
	
	public EmptyTableIterator(Tables tables) {
		this.tables = tables.getTables();
	}
	
	@Override
	public boolean hasNext() {
		while(position < tables.size()) {
			if(tables.get(position).isEmtpy()) {
				return true;
			}
			position++;
		}
		
		return false;
	}
	
	@Override
	public Table next() {
		return tables.get(position++);
	}
	@Override
	public void remove() {
		tables.remove(position);
	}
}

class InUseTableIterator implements Iterator<Table> {
	
	private ArrayList<Table> tables;
	private int position = 0;
	
	public InUseTableIterator(Tables tables) {
		this.tables = tables.getTables();
	}
	
	@Override
	public boolean hasNext() {
		while(position < tables.size()) {
			if(tables.get(position).isInUse()) {
				return true;
			}
			position++;
		}
		
		return false;
	}
	
	@Override
	public Table next() {
		return tables.get(position++);
	}
	@Override
	public void remove() {
		tables.remove(position);
	}
}

class OrderedTableIterator implements Iterator<Table> {
	
	private ArrayList<Table> tables;
	private int position = 0;
	
	public OrderedTableIterator(Tables tables) {
		this.tables = tables.getTables();
	}
	
	@Override
	public boolean hasNext() {
		while(position < tables.size()) {
			if(tables.get(position).isOrderPlaced()) {
				return true;
			}
			position++;
		}
		
		return false;
	}
	
	@Override
	public Table next() {
		return tables.get(position++);
	}
	@Override
	public void remove() {
		tables.remove(position);
	}
}

class UnorderedTableIterator implements Iterator<Table> {
	
	private ArrayList<Table> tables;
	private int position = 0;
	
	public UnorderedTableIterator(Tables tables) {
		this.tables = tables.getTables();
	}
	
	@Override
	public boolean hasNext() {
		while(position < tables.size()) {
			if( tables.get(position).isInUse() &&
			    !tables.get(position).isOrderPlaced() ) {
				return true;
			}
			position++;
		}
		
		return false;
	}
	
	@Override
	public Table next() {
		return tables.get(position++);
	}
	@Override
	public void remove() {
		tables.remove(position);
	}
}